<script>
import { api } from '@/api';
import useContactsStore from '@/stores/contacts';
import { mapStores } from 'pinia';
import { Button, Cell, Dialog, Empty, SwipeCell, Toast } from 'vant';

export default {
  components: {
    Cell,
    Button,
    SwipeCell,
    Empty
},
  computed: {
    ...mapStores(useContactsStore),
    groupId() {
      return this.$route.params.id.replace('@TGS$', '@TGS#')
    }
  },
  data() {
    return {
      list: [],
      grouInfo:{}
    }
  },
  async created() {
    const { data,grouInfo } = await api.group.memberList({ GroupId: this.groupId })
    this.list = data;
    this.grouInfo = grouInfo;
  },
  methods: {
    handleAdd() {
      this.$router.push({
        path: '/create-group',
        query: {
          groupId: this.$route.params.id
        }
      })
    },
    async handleRemove(username) {
      const confirm = await Dialog.confirm({
        title: '提示',
        message: '确定要删除吗？'
      }).catch(() => {})

      if (confirm !== 'confirm') return

      const { ActionStatus } = await api.group.removeMember({
        GroupId: this.groupId,
        Member_Accounts: [{ Member_Account: username }]
      })
      if (ActionStatus === 'OK') {
        Toast.success({ message: '删除成功' })
      } else {
        Toast.fail({ message: '删除失败' })
      }
    },
    async handleRemoveGroup() {
      const confirm = await Dialog.confirm({
        title: '提示',
        message: '确定要删除群聊吗？'
      }).catch(() => {})

      if (confirm !== 'confirm') return

      const { ActionStatus } = await api.group.remove({ GroupId: this.groupId })
      if (ActionStatus === 'OK') {
        Toast.success({ message: '删除成功' })
        this.$router.go(-2)
      } else {
        Toast.fail({ message: '删除失败' })
      }
    }
  },
}
</script>

<template>
  <div class="group-detail">
    <Cell title="群名称" :value="grouInfo.Name" />
    <Cell title="群成员">
      <template #extra>
        <Button size="small" type="primary" @click="handleAdd">添加</Button>
      </template>
    </Cell>

    <div class="contacts-list">
      <SwipeCell
        v-for="item in list" :key="item.username"
      >
        <div class="contacts-item">
          <div class="contacts-item-info">
            <img class="contacts-item-avatar" :src="`/${item.avatar_url}`" alt="">
            <div class="contacts-item-name">
              <div class="contacts-item-nickname">{{ item.nick_name }}</div>
              <div class="contacts-item-uid">UName: {{ item.user_name }}</div>
            </div>
          </div>
        </div>

        <template #right>
          <Button square type="danger" text="删除" style="height: 100%;" @click="handleRemove(item.user_name)" />
        </template>
      </SwipeCell>

      <Empty v-if="list.length === 0" description="暂无成员" />

      <div style="padding: 0 15px; margin: 10px 0;">
        <Button type="danger" block @click="handleRemoveGroup">删除群聊</Button>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.group-detail {
  .contacts-list {
    flex: 1;

    .contacts-item {
      gap: 15px;
      padding: 15px 0;
      margin: 0 15px;

      /deep/ .van-checkbox__label {
        flex: 1;
      }

      .contacts-item-info {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 15px;
        overflow: hidden;
      }

      .contacts-item-send {
        width: 24px;
        height: 24px;
      }

      &-avatar {
        width: 40px;
        height: 40px;
        border-radius: 4px;
        object-fit: cover;
      }

      .contacts-item-name {
        display: flex;
        flex-direction: column;
        gap: 5px;
        font-size: 16px;
        overflow: hidden;

        >div {
          text-overflow: ellipsis;
          overflow: hidden;
          text-wrap: nowrap;
        }

        .contacts-item-uid {
          font-size: 13px;
          color: #999;
        }
      }
    }
  }
}
</style>
